﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SHOP.Areas.Auth.Models;

namespace SHOP.Areas.Auth.Controllers
{
    public class RegisterController : Controller
    {
        //
        // GET: /Auth/Register/

        //Kết nối dữ liệu
        AccountModelDataContext dataTable = new AccountModelDataContext();

        public Constants Constants = new Constants();

        public ActionResult Index()
        {
            return View();
        }

        public bool CheckUser(string username)
        {
            bool st = true;
            int Count = (from c in dataTable.accounts where c.username == username select c).Count();
            if (Count > 0) st = false;

            return st;
        }

        public bool CheckEmail(string email)
        {
            bool st = true;
            int Count = (from c in dataTable.accounts where c.email == email select c).Count();
            if (Count > 0) st = false;

            return st;
        }

        [HandleError, HttpPost]
        public ActionResult Save()
        {
            var json = "success";
            var message = "";

            if (!CheckEmail(Request.Form["email"])) { message = "Email đã tồn tại"; json = "fail"; }
            if (!CheckUser(Request.Form["username"])) { message = "Tên đăng nhập đã tồn tại"; json = "fail"; }
            
            //Insert
            if (ModelState.IsValid && Request.Form["password"].Length >= 5 && Request.Form["password"] != "" && json == "success")
            {
                var ListItem = new account();

                ListItem.username   = Request.Form["username"];
                ListItem.password   = Constants.md5(Request.Form["password"]);
                ListItem.email      = Request.Form["email"];
                ListItem.fullname   = Request.Form["fullname"];
                ListItem.phone      = Request.Form["phone"];
                ListItem.address    = Request.Form["address"];
                ListItem.random_key = Constants.md5(DateTime.Now.ToString());
                ListItem.sendmail   = (Request.Form["sendmail"] != null) ? 1 : 0;
                ListItem.status     = 1;
                ListItem.auth       = 2;
                ListItem.changed    = DateTime.Now;
                ListItem.created    = DateTime.Now;

                dataTable.accounts.InsertOnSubmit(ListItem);
                dataTable.SubmitChanges();

                var UserNew = dataTable.accounts.SingleOrDefault(c => c.username == Request.Form["username"]);
                Session["id"]       = UserNew.id;
                Session["email"]    = UserNew.email;
                Session["username"] = UserNew.username;
                Session["fullname"] = UserNew.fullname;
                Session["auth"]     = UserNew.auth;
            }
            else
            {
                json = "fail";
            }

            return Json(new { st = json, message = message });
        }

    }
}
